package org.leviatan.dataharbour.core.admin;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeSet;
import java.util.Vector;

import org.leviatan.dataharbour.core.model.DataSource;
import org.leviatan.dataharbour.core.model.HarbourRequest;
import org.leviatan.dataharbour.core.model.PersistenceResource;
import org.leviatan.definator.core.model.Table;

/**
 * This is the admin interface. Admin clients of DataHarbour always 
 * connects through this interface.
 * 
 * @author Leviatan
 *
 */
public abstract interface DataHarbourAdminService {
	
	// DataHarbour unique id (key) handling
	///////////////////////////////////////////////////////////////////////////
	/**
	 * Gets a new generated unique id.
	 * @return	A unique id.
	 */
	public abstract String getNewUniqueId();
	

	// saves
	///////////////////////////////////////////////////////////////////////////
	/**
	 * Saves all DataSource objects to DataHarbour's file area.
	 */
	public abstract void saveDataSources();
	
	/**
	 * Saves all PersistenceResource objects to DataHarbour's file area.
	 */
	public abstract void savePersistenceResources();
	
	/**
	 * Saves all unpersisted HarbourRequest objects to DataHarbour's file area.
	 */
	public abstract void saveUnpersistedHarbourRequests();
	
	
	// main loads
	///////////////////////////////////////////////////////////////////////////
	
	/**
	 * Loads specified configuration.
	 */
	public abstract void loadConfiguration(String filePath);
	
	/**
	 * Saves all data models to file system.
	 */
	public abstract void saveModels();
	
	/**
	 * Loads all data models from file system into Register.
	 */
	public abstract void loadModels();
	
	/**
	 * Loads all mappings from file system into Register.
	 */
	public abstract void loadRegisterMappings();
	
	/**
	 * Saves whole Register to file system.
	 */
	public abstract void saveRegister();
	
	/**
	 * Saves all current mappings between DataSource ids and Definator table ids
	 * to file system.
	 */
	public abstract void saveDataSource2DefinitionsMappings();
	
	/**
	 * Saves all current mappings between DataSource ids and PersistenceResource ids
	 * to file system.
	 */
	public abstract void saveDataSource2PersistenceResourceMappings();
	
	
	
	//	 gets
	///////////////////////////////////////////////////////////////////////////
	
	/**
	 * Gets the PersistenceResource object related to the specified id.
	 * @return	A PersistenceResource object. If there is no object with the
	 * specified key, null will be returned.
	 */
	public abstract PersistenceResource getPersistenceResource(String persResId);
	
	/**
	 * Gets all unpersisted HarbourRequest objects.
	 * 
	 * @return	A Map of <(String) HarbourRequest id, HarbourRequest>
	 */
	public abstract Map getAllUnpersistedHarbourRequests();

	/**
	 * Gets the HarbourRequest objects mapped to specified sender id.
	 * 
	 * @param senderId	The sender id.
	 * @return	A Collection of HarbourRequest objects.
	 */
	public abstract Collection getSendersUnpersistedHarbourRequests(String senderId);
	
	/**
	 * Gets specified HarbourRequest from Register.
	 * 
	 * @param reqId	The id of the HarbourRequest to get.
	 * @return	A HarbourRequest object.
	 */
	public abstract HarbourRequest getUnpersistedHarbourRequest(String reqId);
	
	/**
	 * Gets specified DataSource from Register.
	 * 
	 * @param id	The id of the DataSource to get.
	 * @return	A DataSource object.
	 */
	public abstract DataSource getDataSource(String id);
	
	/**
	 * Gets all the DataSource objects in Register.
	 * 
	 * @return	A map containing <(String)DataSource id, DataSource>
	 */
	public abstract Map getAllDataSources();
	
	/**
	 * Gets all Table definitions mapped to specified DataSource.
	 * 
	 * @param dataSourceId	The DataSource id.
	 * @return	A Vector of Table definition keys.
	 */
	public abstract Vector getDataSourcesDefs(String dataSourceId);
	
	/**
	 * Gets all the PersistenceResource keys mapped to the specified DataSource key.
	 * 
	 * @param dataSourceId	The DataSource key.
	 * @return	A Vector containing PersistenceResource keys.
	 */
	public abstract TreeSet<String> getDataSourcesPersistenceResourceKeys(String dataSourceId);
	
	/**
	 * Gets all the PersistenceResource objects mapped to the specified key.
	 * 
	 * @param dataSourceId	The DataSource key.
	 * @return	A Vector containing PersistenceResource objects.
	 */
	public abstract TreeSet<String> getDataSourcesPersistenceResources(String dataSourceId);
	
	/**
	 * Gets all virtual structure objects, i.e. <link>Table</link> objects.
	 * 
	 * @return	A Collection of Table objects.
	 */
	public abstract Collection<Table> getAllVirtualStructures();
	
	/**
	 * Gets all PersistenceResource objects.
	 * 
	 * @return	A Vector of PersistenceResource objects.
	 */
	public abstract HashMap getAllPersistenceResources();
	
	
	
	
	//	 mapping adds
	///////////////////////////////////////////////////////////////////////////
	/**
	 * Adds a mapping between the specified DataSource and Definator Table.
	 * 
	 * @param dsId	The DataSource key.
	 * @param defId	The Definator Table key.
	 */
	public abstract void addDefToDataSource(String dsId, String defId);
	
	/**
	 * Adds a mapping between specified DataSource and PersistenceResource.
	 * 
	 * @param dsId	The DataSource id.
	 * @param perResId	The PersistenceResource id.
	 */
	public abstract void addPersistenceResourceToDataSource(String dsId, String perResId);
	
	
	//	 removes
	///////////////////////////////////////////////////////////////////////////
	/**
	 * Removes a DataSource object from Register.
	 * 
	 * @param id	The id of the DataSource to remove.
	 */
	public abstract void removeDataSource(String id);
	
	/**
	 * Removes a HarbourRequest object from Register.
	 * 
	 * @param reqId	The id of the HarbourRequest to remove.
	 */
	public abstract void removeUnpersistedHarbourRequest(String reqId);
	
	/**
	 * Removes mapping between specified DataSource and Table definition.
	 * 
	 * @param dataSourceId	The id of the DataSource.
	 * @param defId	The id of the Definator Table.
	 */
	public abstract void removeDefFromDataSource(String dataSourceId, String defId);
	
	/**
	 * Removes mapping between specified DataSource and PersistenceResource.
	 * 
	 * @param dataSourceId	The DataSource id.
	 * @param persResId	The PersistenceResource id.
	 */
	public abstract void removePersistenceResourceFromDataSource(
			String dataSourceId, String persResId);
	
	/**
	 * Removes the PersistenceResource object with the specified key from from
	 * DataHarbour's register.
	 * 
	 * @param persResId	The id of the PersistenceResource to remove.
	 */
	public abstract void removePersistenceResource(String persResId);
	
	//	 updates
	///////////////////////////////////////////////////////////////////////////
	
	/**
	 * Updates the name of the specified DataSource object.
	 * 
	 * @param id	The id of the DataSource object to update.
	 * @param name	The name to set for the DataSource.
	 */
	public abstract void updateDataSource(String id, String name);
	
	/**
	 * Updates the name of the specified PersistenceResource object.
	 * 
	 * @param id	The id of the PersistenceResource object to update.
	 * @param name	The name to update with.
	 */
	public abstract void updatePersistenceResource(String id, String name);
	
	/**
	 * Updates the name and configuration of the specified HibernatePersistenceResource.
	 * 
	 * @param id	The id of the HibernatePersistenceResource to update.
	 * @param name	The new name to set.
	 * @param hibConf	The new configuration string to set.
	 */
	public abstract void updateHibernatePersistenceResource(String id, String name, String hibConf);

	
	//	 adds of objects
	///////////////////////////////////////////////////////////////////////////
	
	/**
	 * Adds the specified DataSource object to DataHarbour.
	 * 
	 * @return	The id of the added DataSource object.
	 */
	public abstract String addDataSource(DataSource ds);
	
	/**
	 * Creates a new DataSource object with the specified name and adds it to
	 * DataHarbour's register.
	 * 
	 * @param name	The name of the new DataSource.
	 * @return	The id of the created DataSource object.
	 */
	public abstract String createDataSource(String name);
	
	/**
	 * Creates a new PersistenceResource object with the specified name and of
	 * the specified type, and adds it to DataHarbour's register.
	 * 
	 * @param name	The name to set.
	 * @param type	The type to create.
	 * @return	The id of the created PersistenceResource object.
	 */
	public abstract String createPersistenceResource(String name, String type);
	
	/**
	 * Creates a new HibernatePersistenceResource object with the specified name
	 * and configuration, and adds it to DataHarbour's register.
	 * 
	 * @param name	The name to set.
	 * @param confString	The configuration to set.
	 * @return	The id of the created HibernatePersistenceResource object.
	 */
	public abstract String createHibernatePersistenceResource(String name, String confString);
	
}
